namespace ui {
	// auto visiable
	export interface ProgressBarCallbackParam {
		text?: string;
		value: number;
		time: number;
		bottom?: number;
		callback?: Function;
	}

	export class ProgressBar extends eui.ProgressBar {
		constructor() {
			super();
			this.skinName = 'progressBar_collect_loading';
		}

		// private gAni: eui.Group;

		// private gQing: eui.Group;

		public static mainBar(): ProgressBar {
			if (!this.main) {
				this.main = new ProgressBar();
			}
			return this.main;
		}

		private static main: ProgressBar;

		setProgress(time: number, callback?: Function): void {
			this.visible = true;
			this.value = 0;
			this.stoped = false;
			time = time || 3000;
			// 第四个参数为true ，移除掉之前的选项！
			const tw = egret.Tween.get(this, null, null, true);
			tw.to({ value: 100 }, time).call(() => {
				this.onClose();
				if (!this.stoped && callback) {
					callback();
				}
			});
		}

		times = 0;

		setProgressEx(params: ProgressBarCallbackParam[]): void {
			this.visible = true;
			this.value = 0;
			if (params.length <= 0) {
				return;
			}
			this.stoped = false;
			this._setProgressEx(params, this.times++);
		}

		lbText: eui.Label;

		_setProgressEx(params: ProgressBarCallbackParam[], times: number): void {
			const tw = egret.Tween.get(this, null, null, true);
			const param = params.shift();
			console.log('ProgressBar._setProgressEx', times, param.value);
			if (param.text) {
				this.lbText.text = param.text;
				this.lbText.visible = true;
			} else {
				this.lbText.visible = false;
			}

			tw.to({ value: param.value }, param.time).call(() => {
				console.log('ProgressBar.callback', times, param.value, this.stoped);
				if (this.stoped) {
					return;
				}
				if (param.callback) {
					param.callback(param.value);
				}
				if (params.length > 0) {
					this._setProgressEx(params, times);
				} else {
					this.stopProgress();
				}
			});
		}

		stoped = false;

		stopProgress(): void {
			// console.log('ProgressBar.stopProgress');
			this.onClose();
			this.stoped = true;

			egret.Tween.removeTweens(this);
		}

		onClose(): void {
			this.visible = false;
		}
	}

	export function showProgressOnce(
		parent: egret.DisplayObjectContainer,
		text: string,
		time: number,
		bottom: number,
		callback: Function,
	) {
		const bar = new ui.ProgressBar();
		bar.horizontalCenter = 0;
		bar.bottom = bottom || 100;
		parent.addChild(bar);
		// DlgMain.ins().addChild(bar);
		bar.setProgressEx([{ text, value: 100, time, callback }]);
		return bar;
	}

	export function showMainProgressPromise(param: ProgressBarCallbackParam): Promise<unknown> {
		const bar = ui.ProgressBar.mainBar();
		bar.horizontalCenter = 0;
		bar.bottom = param.bottom || 100;
		if (bar.parent == null) {
			DlgMain.ins().addChild(bar);
		}
		const p = new Promise((resolve, reject) => {
			param.callback = resolve;
			bar.setProgressEx([param]);
		});
		return p;
	}

	export function showModalProgressPromise(param: ProgressBarCallbackParam): Promise<unknown> {
		const bar = new ui.ProgressBar();
		bar.horizontalCenter = 0;
		bar.bottom = param.bottom || 100;
		MainWindow.ins().modal.showModal(bar);
		const p = new Promise((resolve, reject) => {
			param.callback = () => {
				MainWindow.ins().modal.doRemoveModal(bar);
				resolve(0);
			};
			bar.setProgressEx([param]);
		});
		return p;
	}
}
